<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>
              江苏传智播客教育科技股份有限公司
            </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">
                      添加子部门
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--  -->
        <el-tree :data="list">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>
                  {{ data.name }}
                </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="hEidet(data.id)">
                          编辑子部门
                        </el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >
                          删除子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEidet ? '编辑部门' : '新增子部门'"
        :visible.sync="showDialog"
      >
        <addOrEdit
          v-if="showDialog"
          :id="curId"
          :origin-list="originList"
          :is-eidet="isEidet"
          @close="showDialog = false"
          @success="hsuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      showDialog: false, // 弹框的关闭和开启
      list: [],
      curId: '',
      isEidet: false // false是新增     true 是 编辑
    }
  },
  // 调用函数,获取数据
  created() {
    this.loadDepartments()
  },
  methods: {
    // 点击删除部门
    hDel(id) {
      this.$confirm('是否要删除此部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartment(id)
          if (res.success) {
            this.loadDepartments()
          }
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 子传父   关闭弹框
    hsuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hEidet(id) {
      this.curId = id
      this.isEidet = true
      this.showDialog = true
    },
    // 父传子  传id
    hAdd(id) {
      this.curId = id
      this.isEidet = false // 切换是编辑还是新增
      this.showDialog = true
    },
    // 获取组织架构列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        this.originList = res.data.depts.map((item) => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList)
        res.data.depts.shift() // 删除数组的第一个数据   公司的名称
        this.list = tranListToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
